﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.OpenGL;
using System.Drawing;
using OutlookHub.Utils;
using Microsoft.WindowsMobile.PocketOutlook;
using System.Reflection;
using Silvermoon.UI;
using Silvermoon.Windows.Styles;
using Silvermoon.Images;
using Silvermoon.Shapes;
using Silvermoon.Windows;
using OutlookHub.Images;
using OutlookHub;
using Silvermoon.Navigation;
using OutlookHub.Styles;
using Silvermoon.OpenGL.Native;
using OutlookHub.Controls;
using OutlookHub.Controllers;

namespace OutlookHub.Pages
{
    public class ContactDetailsPage : Page
    {

        private ListView listView;
        private ContactData contact;


        public ContactDetailsPage()
            : base()
        {
            InitializePage();
        }

        public ContactData Contact
        {
            get { return contact; }
            set { SetContact(value); }
        }

        public bool Styled { get; set; }

        private void InitializePage()
        {
            SetTransition(350, TransitionMask.Zoom | TransitionMask.Fade, 500, TransitionMask.Explode | TransitionMask.Fade);
            Title = "CONTACTS < DETAILS";
            Styles = StylePool.Details;

            listView = new ListView { Margin = new Margin(0, 0, 0, 0), ItemHeight = 178, Squeeze = false, KeepItems = true };

            Controls.Add(listView);

            Toolbar tb = new Toolbar();
            Toolbar = tb;
            tb.Buttons.Add(new ToolbarButton { Content = SysImages.Back });
            tb.Buttons.Add(new ToolbarButton { Content = SysImages.RecycleBin, Command = OutlookCommands.DeleteContact });
            tb.Buttons[0].Tap += (s, e) => GoBack();
        }

        private void SetContact(ContactData contactData)
        {
            this.contact = contactData;
            Contact contact = contactData.Contact;

            List<Item> comItems = new List<Item>();

            comItems.Add(new ContactTitleItem(contact));
            foreach (var item in GetContactComItems(contact))
            {
                comItems.Add(item);
            }

            listView.ItemsSource = comItems;
        }


        private IEnumerable<Item> GetContactComItems(Contact contact)
        {
            string home = GetAddress(contact.HomeAddressCity, contact.HomeAddressPostalCode, contact.HomeAddressStreet, contact.HomeAddressState, contact.OtherAddressCountry);
            if (!string.IsNullOrEmpty(home)) yield return new ContactDetailItem("home address", home, ImagePool.Address);

            if (contact.Email1Address != string.Empty) yield return new ContactDetailItem("email", contact.Email1Address, ImagePool.EMail);
            if (contact.Email2Address != string.Empty) yield return new ContactDetailItem("email 2", contact.Email2Address, ImagePool.EMail);
            if (contact.Email3Address != string.Empty) yield return new ContactDetailItem("email 3", contact.Email3Address, ImagePool.EMail);


            if (contact.WebPage != null) yield return new ContactDetailItem("homepage", contact.WebPage.AbsoluteUri, ImagePool.Web);
            if (!string.IsNullOrEmpty(contact.AccountName)) yield return new ContactDetailItem("account name", contact.AccountName);

            if (contact.Birthday.Year > 1601) yield return new ContactDetailItem("birthday", contact.Birthday.ToLongDateString(), ImagePool.Born);

            if (contact.HomeTelephoneNumber != string.Empty) yield return new ContactDetailItem("home number", contact.HomeTelephoneNumber, ImagePool.Phone);
            if (contact.MobileTelephoneNumber != string.Empty) yield return new ContactDetailItem("mobile number", contact.MobileTelephoneNumber, ImagePool.Mobile);
            if (contact.Home2TelephoneNumber != string.Empty) yield return new ContactDetailItem("second home number", contact.Home2TelephoneNumber, ImagePool.Phone);
            if (contact.HomeFaxNumber != string.Empty) yield return new ContactDetailItem("home fax", contact.HomeFaxNumber);
            if (contact.BusinessTelephoneNumber != string.Empty) yield return new ContactDetailItem("work number", contact.BusinessTelephoneNumber, ImagePool.Phone);
            if (contact.Business2TelephoneNumber != string.Empty) yield return new ContactDetailItem("second work  number", contact.Business2TelephoneNumber, ImagePool.Phone);
            if (contact.BusinessFaxNumber != string.Empty) yield return new ContactDetailItem("work fax", contact.BusinessFaxNumber);

            string work = GetAddress(contact.BusinessAddressCity, contact.BusinessAddressPostalCode, contact.BusinessAddressStreet, contact.BusinessAddressState, contact.BusinessAddressCountry);
            if (!string.IsNullOrEmpty(work)) yield return new ContactDetailItem("work address", work, ImagePool.Office);

            string other = GetAddress(contact.OtherAddressCity, contact.OtherAddressPostalCode, contact.OtherAddressStreet, contact.OtherAddressState, contact.OtherAddressCountry);
            if (!string.IsNullOrEmpty(other)) yield return new ContactDetailItem("other address", other, ImagePool.Clip);


            if (!string.IsNullOrEmpty(contact.Body))
            {
                ContainerItem item = new ContainerItem();

                //ScrollStackPanel notesBoxContainer = new ScrollStackPanel { Margin = 10 };
                TextBox notesBox = new TextBox
                {
                    AcceptReturn = true,
                    AutoHeight = true,
                    Margin = new Margin(0, 0, ScrollViewer.ScrollbarWidth, null),
                    Color = PhoneColors.Transparent,
                    TextColor = PhoneColors.White,
                    Text = contact.Body
                };
                item.Control = notesBox;
                yield return item;
                //notesBoxContainer.Controls.Add(notesBox);

            }
        }


        private string GetAddress(string city, string postalCode, string street, string state, string country)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(street);
            string loc = (postalCode + " " + city).Trim();
            if (!string.IsNullOrEmpty(loc))
            {
                if (sb.Length > 0) sb.AppendLine();
                sb.Append(loc);
            }
            if (!string.IsNullOrEmpty(country))
            {
                if (sb.Length > 0) sb.AppendLine();
                sb.Append(country);
            }
            if (!string.IsNullOrEmpty(state))
            {
                if (sb.Length > 0) sb.AppendLine();
                sb.Append(state);
            }
            return sb.ToString();
        }


        protected override void OnCommand(CommandEventArgs args)
        {
            if (args.Command == OutlookCommands.DeleteContact) args.Object = Contact;
            base.OnCommand(args);
        }

        protected override void OnHome(HandledEventArgs e)
        {
            SetTransition(350, TransitionMask.RotateLeftIn, 350, TransitionMask.RotateLeftOut);
            base.OnHome(e);
        }

    }
}
